<?php
declare(strict_types=1);

namespace app\library\traits;

use app\library\facade\Logger;

trait Json
{
    /**
     * @param int $status
     * @param string $message
     * @param array $data
     */
    public function returnJson(int $error = 0, string $message = 'success', array $data = [])
    {
        Logger::channel('api_response')->info('返回数据', [
            'error' => $error,
            'msg' => $message,
            'data' => $data
        ]);
        $response = $this->getResponse();
        $response->setBody(json_encode([
            'error' => $error,
            'msg' => $message,
            'data' => $data
        ]));
        $response->response();
        $response->clearBody();
//        echo json_encode([
//            'error' => $error,
//            'msg' => $message,
//            'data' => $data
//        ]);
    }

    /**
     * @param array $data
     * @param string $message
     */
    public function returnSuccess(array $data = [], string $message = 'success')
    {
        $this->returnJson(0, $message, $data);
        return false;
    }

    /**
     * @param int $error
     * @param string $message
     * @param array $data
     */
    public function returnError(int $error, string $message, array $data = [])
    {
        $this->returnJson($error, $message, $data);
        return false;
    }


}